package rule

import (
	"fmt"
	conf "goclient/src/conf"
	"strings"

	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324"
)

// 类型
type Rule struct {
	Protocol                string
	Port                    string
	CidrBlock               string
	Action                  string
	FirewallRuleDescription string
}

type Rules []Rule

// 连接类型
type Client struct {
	SecretId   string
	SecretKey  string
	InstanceId string
}

// 添加规则
func addRule(client *lighthouse.Client, InstanceId string, rules []*lighthouse.FirewallRuleInfo) {
	// 实例化一个请求对象,每个接口都会对应一个request对象
	request := lighthouse.NewModifyFirewallRulesRequest()

	request.InstanceId = common.StringPtr(InstanceId)
	/*
		request.FirewallRules = []*lighthouse.FirewallRule{
			&lighthouse.FirewallRule{
				Protocol:                common.StringPtr(rule.Protocol),
				Port:                    common.StringPtr(rule.Port),
				CidrBlock:               common.StringPtr(rule.CidrBlock),
				Action:                  common.StringPtr(rule.Action),
				FirewallRuleDescription: common.StringPtr(rule.FirewallRuleDescription),
			},
		}
	*/
	request.FirewallRules = []*lighthouse.FirewallRule{}
	for _, rule := range rules {
		request.FirewallRules = append(request.FirewallRules, &lighthouse.FirewallRule{
			Protocol:                common.StringPtr(*rule.Protocol),
			Port:                    common.StringPtr(*rule.Port),
			CidrBlock:               common.StringPtr(*rule.CidrBlock),
			Action:                  common.StringPtr(*rule.Action),
			FirewallRuleDescription: common.StringPtr(*rule.FirewallRuleDescription),
		})
	}

	// 返回的resp是一个ModifyFirewallRulesResponse的实例，与请求对象对应
	response, err := client.ModifyFirewallRules(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Printf("An API error has returned: %s", err)
		return
	}
	if err != nil {
		panic(err)
	}
	// 输出json格式的字符串回包
	fmt.Printf("添加成功 %s", response.ToJsonString())
}

// 获取到规则
func GetRules(config conf.Device, Ip string) {
	// 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
	// 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
	// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
	credential := common.NewCredential(
		config.Secretid,
		config.Secretkey,
	)
	// 实例化一个client选项，可选的，没有特殊需求可以跳过
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "lighthouse.tencentcloudapi.com"
	// 实例化要请求产品的client对象,clientProfile是可选的
	client, _ := lighthouse.NewClient(credential, "ap-guangzhou", cpf)

	// 实例化一个请求对象,每个接口都会对应一个request对象
	request := lighthouse.NewDescribeFirewallRulesRequest()

	request.InstanceId = common.StringPtr(config.Instanceid)

	// 返回的resp是一个DescribeFirewallRulesResponse的实例，与请求对象对应
	response, err := client.DescribeFirewallRules(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		fmt.Printf("An API error has returned: %s", err)
		return
	}
	if err != nil {
		panic(err)
	}

	flag := false
	// 迭代
	for _, rule := range response.Response.FirewallRuleSet {
		if strings.Index(config.Port, *rule.Port) != -1 && strings.Index(*rule.FirewallRuleDescription, config.Name) != -1 {
			flag = true
			fmt.Printf("%s 需要添加 \n", *rule.FirewallRuleDescription) // *rule.Protocol,
			*rule.FirewallRuleDescription = config.Name + "ip"
			*rule.CidrBlock = Ip
		}
	}
	if flag == true {
		addRule(client, config.Instanceid, response.Response.FirewallRuleSet)
	}
}
